package day01;

import exception.DataAccessException;
import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Arrays;

@WebServlet("/add")
public class AddEmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("AddEmpServlet.service()");

        request.setCharacterEncoding("utf-8");


        // response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        PrintWriter out=response.getWriter();


        //获得表单提交的数据
        String name=request.getParameter("name");



        // name=new String(name.getBytes("iso8859-1"),"utf-8");

        String salaryStr=request.getParameter("salary");
        Double salary="".equals(salaryStr.trim())
                        ?null
                        :Double.parseDouble(salaryStr);
        String sex=request.getParameter("sex");
        String edu=request.getParameter("edu");
        String[] hobbies=request.getParameterValues("hobbies");
        String description=request.getParameter("description");


        System.out.println("name = " + name);
        System.out.println("salary = " + salary);
        System.out.println("sex = " + sex);
        System.out.println("edu = " + edu);
        System.out.println("Arrays.toString(hobbies) = " + Arrays.toString(hobbies));
        System.out.println("description = " + description);



        //将数据保存到数据库
        Connection con=null;
        PreparedStatement ps=null;
        try {
            con=JdbcUtil.getConnection();

            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("insert into t_emp ")
                    .append("    (name,salary,sex,edu,hobbies,description) ")
                    .append("values ")
                    .append("    (?,?,?,?,?,?) ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,name);
            ps.setDouble(2,salary);
            ps.setString(3,sex);
            ps.setString(4,edu);
            ps.setString(5,String.join(",",hobbies));
            ps.setString(6,description);


            ps.executeUpdate();

            con.commit();

            // out.print("<h1>add success</h1>");
            // out.print("<h1>添加成功</h1>");
            response.sendRedirect(request.getContextPath()+"/list");
        } catch (Exception e){
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            out.print("<h1>add fail</h1>");
        } finally {
            JdbcUtil.close(con,ps,null);
        }










    }
}
